package com.nickstumpos.android.easyVolume.streams.presentation;

import java.util.List;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.nickstumpos.android.easyVolume.R;
import com.nickstumpos.android.easyVolume.playRinger.IPlaySample;
import com.nickstumpos.android.easyVolume.streams.AbstractVolumeScreen;
import com.nickstumpos.android.easyVolume.streams.IStreamScreen;
import com.nickstumpos.android.easyVolume.streams.presentation.types.Screen;
import com.nickstumpos.android.easyVolume.volume.controller.IVolumeController;
import com.nickstumpos.android.easyVolume.volume.levels.VibrateLevel;
import com.nickstumpos.android.easyVolume.volume.levels.VolumeLevel;

public class MasterStreamScreen extends AbstractVolumeScreen {

	private class ChoosePlayer extends Dialog {

		private List<IStreamScreen>	players;

		private ListView								list;

		public ChoosePlayer(Context context) {
			super(context);
			this.players = MasterStreamScreen.this.getManager().getSlaves();
			this.list = new ListView(context);
			this.list.setId(this.list.hashCode());
		}

		public ChoosePlayer(Context context, boolean cancelable,
				OnCancelListener cancelListener) {
			super(context, cancelable, cancelListener);
			this.players = MasterStreamScreen.this.getManager().getSlaves();
			this.list = new ListView(context);
			this.list.setId(this.list.hashCode());
		}

		public ChoosePlayer(Context context, int theme) {
			super(context, theme);
			this.players = MasterStreamScreen.this.getManager().getSlaves();
			this.list = new ListView(context);
			this.list.setId(this.list.hashCode());
		}

		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			this.players = MasterStreamScreen.this.getManager().getSlaves();
			ArrayAdapter<String> aa = new ArrayAdapter<String>(
					this.list.getContext(), android.R.layout.simple_list_item_1);
			for (int i = 0; i < this.players.size(); i++) {
				aa.add(this.players.get(i).getName());
			}

			this.list.setAdapter(aa);
			this.list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

				public void onItemClick(AdapterView<?> parentView, View childView,
						int position, long id) {
					ChoosePlayer.this.setResultPosition(position);
				}
			});
			this.setTitle("Choose Slave Stream To Play");
			this.setContentView(this.list);

		}

		private void setResultPosition(int position) {
			MasterStreamScreen.this.startPlayer(this.players.get(position));
		}

	}

	private TextView			slavesText;
	boolean								disabled	= false;

	boolean								playing		= false;

	private ChoosePlayer	pl;
	IPlaySample						playSample;
	private VolumeLevel		lastSetVolume;

	public MasterStreamScreen(Context context) {
		super(context);
		super.setStream(null);
	}

	public MasterStreamScreen(Context context, AttributeSet attrs) {
		super(context, attrs);
		super.setStream(null);
	}

	public MasterStreamScreen(Context context, AttributeSet attrs,
			int defStyle) {
		super(context, attrs, defStyle);
		super.setStream(null);
	}

	@Override
	public void assignButtons(Activity main) {
		this.slavesText = (TextView) main.findViewById(R.id.MasterSlaves);
		this.setHigh((ImageButton) main.findViewById(R.id.MasterHigh));
		this.setMedHigh((ImageButton) main.findViewById(R.id.MasterMedHigh));
		this.setMedium((ImageButton) main.findViewById(R.id.MasterMedium));
		this.setMedLow((ImageButton) main.findViewById(R.id.MasterMedLow));
		this.setLow((ImageButton) main.findViewById(R.id.MasterLow));
		this.setSilent((ImageButton) main.findViewById(R.id.MasterSilent));
		super
				.setVolumeStatus((TextView) main.findViewById(R.id.MasterVolumeStatus));
		this.setPlayMedia((ImageButton) main.findViewById(R.id.MasterPlay));
		this.setVibeButton((ImageButton) main.findViewById(R.id.MasterVibrate));
		this
				.setVibrateOnly((ImageButton) main.findViewById(R.id.MasterVibrateOnly));
		this.setTouchListeners();
	}



	/**
	 * @return the lastSetVolume
	 */
	@Override
	protected VolumeLevel getLastSetVolume() {
		return this.lastSetVolume;
	}

	@Override
	public String getName() {
		return Screen.MASTER.getName();
	}

	/**
	 * @return the playing
	 */
	@Override
	protected boolean isPlaying() {
		return this.playing;
	}

	@Override
	public void refresh() {
		this.disabled = false;
		this.setInitialLevel();
	}

	@Override
	public void setInitialLevel() {

		if (!this.disabled) {
			List<IStreamScreen> slaves = super.getManager().getSlaves();

			boolean vibe = false;
			String slaveText = "";
			for (int i = 0; i < slaves.size(); i++) {
				if (slaves.get(i).getName().equals(Screen.NOTIFICATION.getName())) {
					vibe = true;
				} else if (slaves.get(i).getName().equals(Screen.RINGER.getName())) {
					vibe = true;
					this.lastSetVolume = this.getStreamController().getCurrentVolumeLevel(slaves.get(i).getStream());
				}
				slaveText += slaves.get(i).getName();
				if (i + 1 != slaves.size()) {
					slaveText += "\n";
				}
			}

			this.slavesText.setText(slaveText);
			if (vibe) {
				this.enableVibeButton();
			} else {
				this.disableVibeButton();
			}
			if (slaves.size() > 0) {
				if (this.lastSetVolume == null) {
					this.lastSetVolume = this.getStreamController().getCurrentVolumeLevel(slaves.get(0).getStream());
				}
				this.disabled = false;
				this.enableButtons();
				VolumeLevel current = VolumeLevel.NONE.equals(this.lastSetVolume) ? this.getStreamController().getCurrentVolumeLevel(slaves.get(0).getStream())
						: this.lastSetVolume;
				this.setStreamVolumeLevel(current);
			} else {
				this.disabled = true;
				this.disableButtons();
				this.setStreamVolumeLevel(VolumeLevel.NONE);
			}
		}
	}

	/**
	 * @param lastSetVolume
	 *          the lastSetVolume to set
	 */
	protected void setLastSetVolume(VolumeLevel lastSetVolume) {
		this.lastSetVolume = lastSetVolume;
	}

	/**
	 * @param playing
	 *          the playing to set
	 */
	@Override
	protected void setPlaying(boolean playing) {
		this.playing = playing;
	}

	protected void setPlaySample() {

	}

	@Override
	protected void setTouchListeners() {
		if (super.getVibeButton() != null) {
			super.getVibeButton().setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View v) {
					if (!MasterStreamScreen.this.isVibeDisabled()) {
						if (MasterStreamScreen.this.isVibrateSwitch()) {
							MasterStreamScreen.this.setVibrate(VibrateLevel.VIBE_OFF);
							MasterStreamScreen.this.getManager().setSlaveVibrate(
									VibrateLevel.VIBE_OFF);
							MasterStreamScreen.this.setVibrateSwitch(false);
							MasterStreamScreen.this.setVibeState(false);
						} else {
							MasterStreamScreen.this.setVibrate(VibrateLevel.VIBE_ON);
							MasterStreamScreen.this.setVibrateSwitch(true);
							MasterStreamScreen.this.getVibeButton().setImageResource(
									R.drawable.vibe_on);
							MasterStreamScreen.this.setVibeState(true);
							MasterStreamScreen.this.getManager().setSlaveVibrate(
									VibrateLevel.VIBE_ON);
						}
					}

				}
			});
		}
		if (this.getPlayMedia() != null) {
			this.getPlayMedia().setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					if (MasterStreamScreen.this.playing) {
						MasterStreamScreen.this.playing = false;
						MasterStreamScreen.this.playSample.stop();
						MasterStreamScreen.this.getPlayMedia().setImageResource(
								R.drawable.button_start);
					} else {
						MasterStreamScreen.this.pl = new ChoosePlayer(
								MasterStreamScreen.this.getContext());
						MasterStreamScreen.this.pl.show();
					}
				}
			});
		}
		if (this.getHigh() != null) {
			this.getHigh().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.HIGH);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.HIGH);
						}
					}
					return false;
				}
			});
		}
		if (this.getMedHigh() != null) {
			this.getMedHigh().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.MED_HIGH);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.MED_HIGH);
						}
					}
					return false;
				}
			});
		}
		if (this.getMedium() != null) {
			this.getMedium().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.MEDIUM);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.MEDIUM);
						}
					}
					return false;
				}
			});
		}
		if (this.getMedLow() != null) {
			this.getMedLow().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.MED_LOW);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.MED_LOW);
						}
					}
					return false;
				}
			});
		}
		if (this.getLow() != null) {
			this.getLow().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.LOW);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.LOW);
						}
					}
					return false;
				}
			});
		}
		if (this.getVibrateOnly() != null) {
			this.getVibrateOnly().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.VIBE_ONLY);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.VIBE_ONLY);
						}
					}
					return false;
				}
			});
		}
		if (this.getSilent() != null) {
			this.getSilent().setOnTouchListener(new View.OnTouchListener() {

				@Override
				public boolean onTouch(final View v, final MotionEvent event) {
					if (!MasterStreamScreen.this.isInactive()) {
						if (MotionEvent.ACTION_DOWN == event.getAction()) {
							MasterStreamScreen.this.setStreamVolumeLevel(VolumeLevel.SILENT);
							MasterStreamScreen.this.getManager().setSlaveVolume(
									VolumeLevel.SILENT);
						}
					}
					return false;
				}
			});
		}
	}

	@Override
	public void setStreamVolumeLevel(VolumeLevel level) {
		this.lastSetVolume = level;
		if (VolumeLevel.VIBE_ONLY.equals(level)) {
			this.setVibeState(true);
			this.disableVibeButton();
		} else if (VolumeLevel.SILENT.equals(level)) {
			this.setVibeState(false);
			this.disableVibeButton();
		} else {
			this.enableVibeButton();
		}
		this.getManager().setSlaveVolume(level);
		this.setButtonLevel(level);
	}

	private void startPlayer(IVolumeController player) {
		this.playing = true;
		this.pl.dismiss();
		this.playSample = player.getPlaySample();
		this.playSample.start();
		MasterStreamScreen.this.getPlayMedia().setImageResource(
				R.drawable.button_stop);
	};
}
